package data.meto;

import java.util.List;

import data.Table.Page;
import data.Table.Table;
import file_rw.FileRW;

public class page {
	public int pagenum;
	public int pagetype;//叶子类型,1:B+leaf,2:B+nleaf,4:b-leaf,8:b-nleaf
	public int firstFreeBlock;//第一个自由块
	public int numOfCells ;//单元数
	public int cellsOffset ;//单元起始地址
	public int emptyPicesNum ;//碎片数
	public int rightChild ;//最右孩子
	public List<Integer> pointer;
	public List<cell> cells;
	
	public byte[] pageByte = new byte[Table.table_page_defaultsize];
	
	public page(int n){
		pagenum = n;	
		Page p=new Page(n);
		pageByte = p.read();
		FileRW fr = new FileRW();
		pagetype = fr.readIntFromByte(pageByte, 0, 1);
		firstFreeBlock = fr.readIntFromByte(pageByte, 1, 2);
		numOfCells = fr.readIntFromByte(pageByte, 3, 2);
		cellsOffset = fr.readIntFromByte(pageByte, 5, 2);
		emptyPicesNum = fr.readIntFromByte(pageByte, 7, 1);
		if(pagetype == 2||pagetype == 8){
			rightChild =  fr.readIntFromByte(pageByte, 8, 4);
		}
		
		for(int i = 0;i<numOfCells;i++){
			pointer.add(fr.readIntFromByte(pageByte, 12+i*2,2 ));
		}
		for(int i=0;i<numOfCells;i++)
		{
			cell acell=new cell();
			int celllength=fr.readIntFromByte(pageByte, pointer.get(i), 2);//get the length of cell[i]
			byte[] cellbyte=new byte[celllength];
			int k=0;
			for(int j=pointer.get(i);j<pointer.get(i)+celllength;j++)
			{
				cellbyte[k]=pageByte[j];
				k++;
			}
			int numofcolumn=fr.readIntFromByte(cellbyte, 1, 2);//number of column
			acell.celllength=celllength;
			acell.N=numofcolumn; 
			int flagcount=0;
			for(int j=0;j<numofcolumn;j++)
			{
				int flag=fr.readIntFromByte(cellbyte, 5, 2);
				acell.elementtype.add(flag);
				if(flag==1){//int
					int element=fr.readIntFromByte(cellbyte, 7+flagcount, 4);
					acell.elements.add(element);
					flagcount+=4;
				}
				else if(flag==2)//long
				{
					long element=fr.readLongFromByte(cellbyte, 7+flagcount);
							//fr.readIntFromByte(cellbyte, 7+flagcount, 8);
					acell.elements.add(element);
					flagcount+=8;
				}
				/*else if(flag==4)//double
				{
					fr.readIntFromByte(cellbyte, 7+flagcount, 8);
					flagcount+=8;
				}*/
				else
				{
					flag=flag-10;
					String element=fr.readStrFromByte(cellbyte, 7+flagcount, flag);
					//fr.readIntFromByte(cellbyte, 7+flagcount, flag);
					acell.elements.add(element);
					flagcount+=flag;
				}
			}
			acell.pointer=fr.readIntFromByte(cellbyte, 2+2+numofcolumn*2+flagcount, 4);
		}
	}
	
	public page(){
		
	}
	
	
	
	public int getPagetype() {
		return pagetype;
	}

	public void setPagetype(int pagetype) {
		this.pagetype = pagetype;
	}

	public int getFirstFreeBlock() {
		return firstFreeBlock;
	}

	public void setFirstFreeBlock(int firstFreeBlock) {
		this.firstFreeBlock = firstFreeBlock;
	}

	public int getNumOfCells() {
		return numOfCells;
	}

	public void setNumOfCells(int numOfCells) {
		this.numOfCells = numOfCells;
	}

	public int getCellsOffset() {
		return cellsOffset;
	}

	public void setCellsOffset(int cellsOffset) {
		this.cellsOffset = cellsOffset;
	}

	public int getEmptyPicesNum() {
		return emptyPicesNum;
	}

	public void setEmptyPicesNum(int emptyPicesNum) {
		this.emptyPicesNum = emptyPicesNum;
	}

	public int getRightChild() {
		return rightChild;
	}

	public void setRightChild(int rightChild) {
		this.rightChild = rightChild;
	}

	public List<Integer> getPointer() {
		return pointer;
	}

	public void setPointer(List<Integer> pointer) {
		this.pointer = pointer;
	}

	public List<cell> getCells() {
		return cells;
	}

	public void setCells(List<cell> cells) {
		this.cells = cells;
	}

	
}
